package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelCommend;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.domain.StrategyTemplate;
import cn.wolfcode.luowowo.search.domain.TravelTemplate;
import cn.wolfcode.luowowo.search.domain.UserInfoTemplate;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.vo.StatisSearchVO;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class IndexController {

    //注意不要贴错注解!!!
    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private ITravelCommendService travelCommendService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IDestinationSearchService destinationSearchService;

    @Reference
    private IStrategySearchService strategySearchService;

    @Reference
    private IUserInfoSearchService userInfoSearchService;

    @Reference
    private ITravelSearchService travelSearchService;

    @Reference
    private ISearchService searchService;

    /**
     * 搜索功能
     *
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/q")
    public String search(Model model, @ModelAttribute("qo") SearchQueryObject qo) {
        if (qo.getKeyword() == null ) {
            return "";
        }
        //要记得是return!!!
        switch (qo.getType()) {
            case SearchQueryObject.SEARCH_TYPE_DEST:
                return searchDests(model, qo);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL:
                return searchTravels(model, qo);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY:
                return searchStrategy(model, qo);
            case SearchQueryObject.SEARCH_TYPE_USER:
                return searchUser(model, qo);
            default:
                return searchAll(model, qo);
        }
    }

    //搜索用户
    private String searchUser(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> page = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("page",page);
        return "index/searchUser";
    }

    //搜索攻略
    private String searchStrategy(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> page = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle","summary");
        model.addAttribute("page",page);
        return "index/searchStrategy";
    }

    //搜索游记
    private String searchTravels(Model model, SearchQueryObject qo) {
        Page<TravelTemplate> page = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");

        model.addAttribute("page",page);

        return "index/searchTravel";
    }

    //搜索所有
    private String searchAll(Model model, SearchQueryObject qo) {
        StatisSearchVO data = new StatisSearchVO();
        //用户
        Page<UserInfoTemplate> user = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");
        data.setTotal(data.getTotal() + user.getTotalElements());

        //攻略
        Page<StrategyTemplate> strategy = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle","summary");
        data.setTotal(data.getTotal() + strategy.getTotalElements());
        //游记
        Page<TravelTemplate> travel = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");

        data.setTotal(data.getTotal() + travel.getTotalElements());
        //目的地
        Page<DestinationTemplate> dest = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME,
                DestinationTemplate.class, qo, "name", "info");
        data.setTotal(data.getTotal() + dest.getTotalElements());

        data.setUsers(user.getContent());
        data.setTravels(travel.getContent());
        data.setStrategys(strategy.getContent());
        data.setDests(dest.getContent());

        model.addAttribute("data",data);

        return "index/searchAll";
    }

    //搜索目的地及关联的数据
    private String searchDests(Model model, SearchQueryObject qo) {
        DestinationTemplate dest = destinationSearchService.findByName(qo.getKeyword());
        //判断是否存在该目的地
        if (dest != null) {
            StatisSearchVO data = new StatisSearchVO();

            List<StrategyTemplate> strategys = strategySearchService.findByDestName(dest.getName());
            data.setTotal(data.getTotal() + strategys.size());

            List<TravelTemplate> travels = travelSearchService.findByDestName(dest.getName());
            data.setTotal(data.getTotal() + travels.size());

            List<UserInfoTemplate> usrs = userInfoSearchService.findByDestName(dest.getName());
            data.setTotal(data.getTotal() + usrs.size());

            data.setStrategys(strategys);
            data.setTravels(travels);
            data.setUsers(usrs);

            model.addAttribute("data", data);
        }
        model.addAttribute("dest", dest);

        return "index/searchDest";
    }


    /**
     * 主页
     * 获取用户信息,并共享
     */
    @RequestMapping("")
    public String index(@UserParam UserInfo userInfo, Model model) {
        //我们的拦截器已经共享了session,所以这里可以不配
/*        String token = CookieUtil.getCookie(request, response);
        UserInfo userInfo = userInfoRedisService.getUserByToken(token);
        request.getSession().setAttribute("userInfo", userInfo);*/

        //tcs
        List<TravelCommend> tcs = travelCommendService.getTop5();
        model.addAttribute("tcs", tcs);

        //sc
        StrategyCommend strategyCommend = strategyCommendService.getTop5().get(0);
        model.addAttribute("sc", strategyCommend);

        //共享用户id
        if(userInfo != null) {
            model.addAttribute("userId", userInfo.getId());
        }
        return "index/index";
    }



    /**
     * 主页游记异步分页
     *
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo") TravelQuery qo) {

        PageInfo<Travel> pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        return "index/travelPageTpl";
    }

}
